import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.expressions.Window

object 窗口函数求差值 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[6]")
      .appName("Test")
      .getOrCreate()

    import spark.implicits._
    import org.apache.spark.sql.functions._

    val source = Seq(
      ("Thin", "Cell phone", 6000),
      ("Normal", "Tablet", 1500),
      ("Mini", "Tablet", 5500),
      ("Ultra thin", "Cell phone", 5000),
      ("Very thin", "Cell phone", 6000),
      ("Big", "Tablet", 2500),
      ("Bendable", "Cell phone", 3000),
      ("Foldable", "Cell phone", 3000),
      ("Pro", "Tablet", 4500),
      ("Pro2", "Tablet", 6500)
    ).toDF("product", "category", "revenue")

    val window = Window.partitionBy('category)
      .orderBy('revenue.desc)
    val maxPerice = max('revenue) over window
    source.select('product,'category,'revenue,(maxPerice - 'revenue) as "revenue_diff")
      .show()
  }
}
